package com.eight.cloud.payment.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eight.cloud.core.utils.supply.UserContext;
import com.eight.cloud.mybatisplus.entity.TenantEntity;
import com.eight.cloud.mybatisplus.service.BaseService;
import com.eight.cloud.openfeign.admin.api.TenantClient;
import com.eight.cloud.openfeign.admin.vo.TenantVo;
import com.eight.cloud.payment.entity.MerchantEntity;
import com.eight.cloud.payment.mapper.MerchantMapper;
import com.eight.cloud.payment.model.dto.MerchantDto;
import com.eight.cloud.payment.service.IMerchantService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @ClassName: MerchantServiceImpl
 * @Author: TXC
 * @Date: 2024-10-21 17:18
 **/
@Service
public class MerchantServiceImpl extends BaseService<MerchantMapper, MerchantEntity> implements IMerchantService {
    @Resource
    private TenantClient tenantClient;
    @Override
    public MerchantEntity saveAndUpdate(MerchantDto merchant) {
        MerchantEntity entity = new MerchantEntity();
        BeanUtils.copyProperties(merchant, entity);
        if (ObjectUtil.isNull(entity.getId())) {
            entity.setCreateTime(LocalDateTime.now());
            save(entity);
        }
        entity.setUpdateTime(LocalDateTime.now());
        if (ObjectUtil.isNull(entity.getUuid())) {
            if (StrUtil.isBlank(merchant.getTenantId())) {
                entity.setTenantId(UserContext.getUser().getTenantId());
            }
            if (StrUtil.isBlank(entity.getTenantId())) {
                throw new RuntimeException("商户号不可为空");
            }
            save(entity);
        } else {
            entity.setTenantId(null);
            updateByUuid(entity);
        }
        return entity;
    }

    @Override
    public MerchantEntity getBySubMerchantId(String subMerchantId) {
        MerchantEntity merchant = baseMapper.selectOne(lambdaQueryWrapper()
                .eq(MerchantEntity::getSubMerchantId, subMerchantId)
                .eq(MerchantEntity::getStatus, 1));
        if (ObjectUtil.isNull(merchant)) {
            throw new RuntimeException("商户号不可用");
        }
        return merchant;
    }

    @Override
    public MerchantEntity getBySubMerchantId(String tenantUuid, String subMerchantId) {
        MerchantEntity merchant = baseMapper.selectOne(lambdaQueryWrapper()
                .eq(MerchantEntity::getSubMerchantId, subMerchantId)
                .eq(TenantEntity::getTenantId, tenantUuid)
                .eq(MerchantEntity::getStatus, 1));
        if (ObjectUtil.isNull(merchant)) {
            throw new RuntimeException("商户号不可用");
        }
        return merchant;
    }

    /**
     * 通过子商户id查找租户信息
     * @param subMerchantId 子商户id
     * @return 租户信息
     */
    @Override
    public TenantVo getTenantBySubMerchantId(String subMerchantId) {
        MerchantEntity merchant = getBySubMerchantId(subMerchantId);
        return tenantClient.query(merchant.getTenantId());
    }
}
